import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Rick Class
 * @author Hugo Armando Rodríguez Flores
 * @version May 23th 2013
 */
public class Rick extends Ordinario
{
    private boolean stoppedSpinning;    //Become true when Rick has stopped spinning
    
    /**
     * Gives the properties to an instance of this class.
     */
    public Rick()
    {
        super();
        super.storeImages(6, "rick");
        stoppedSpinning=false;
    }
    
    /**
     * Makes Rick act in the current world
     */
    public void act() 
    {
        if (!this.getRemoveFlag())
        {
            super.thomasDetection(20);
            super.attackTiming(120);
            if (super.getAttackTiming()==0)
                stoppedSpinning=false;
            super.act();
        }
        else
            super.removeEnemy();
    }
    
    /**
     * This defines how Rick it is going to attack an instance of Thomas class.
     */
    public void attack()
    {
        Actor decPiece=null;
        if (!stoppedSpinning)
            switch(super.getAttackCounter())
            {
                case 0: this.setImage(super.getElement(2));  super.attackCounterIncr(); stoppedSpinning=false; break;
                case 8: this.setImage(super.getElement(3));  super.attackCounterIncr(); stoppedSpinning=false; break;
                case 16: this.setImage(super.getElement(4));  super.attackCounterIncr(); stoppedSpinning=false; break;
                case 24: this.setImage(super.getElement(5));  super.attackCounterIncr(); stoppedSpinning=false; break;
                case 32: super.setAttackCounter(0); 
                    super.setAttackedFlag(true); 
                    this.setImage(super.getElement(0)); 
                    stoppedSpinning=true;
                    break;
                default: super.attackCounterIncr(); stoppedSpinning=true;
            }
        if (!super.getAttackedFlag() && !stoppedSpinning)
            if (!super.getDirectionFlag())//if no change in direction
            {
                if (!super.getDirection())
                {
                    decPiece=getOneObjectAtOffset(-1, 0, Decoracion.class);
                    if (decPiece==null)
                        this.setLocation(this.getX()-1, this.getY());
                }
                else
                {
                    decPiece=getOneObjectAtOffset(1, 0, Decoracion.class);
                    if (decPiece==null)
                        this.setLocation(this.getX()+1, this.getY());
                }
                Actor thomas=getOneIntersectingObject(Thomas.class);
                if (thomas!=null)
                    ((Thomas)thomas).lowLifePercentageFromOutside(5);
                super.setDirectionFlag(super.getDirectionFlag());
            }
    }
}
